﻿using Business.Base;
using Business.Base.Common;
using Business.Base.Enums;
using Business.Base.Model;
using Business.Base.Service;
using Business.NPSH;

using Configuration;
using System;
using System.Collections.Generic;

using System.Windows.Forms;

namespace Pumptester.ExperimentForms.Npsh
{
    public partial class CreateForm : Pumptester.ExperimentForms.Base.BaseCreateForm
    {
        //const ExperimentType experimentType = ExperimentType.Npsh;
        //const PumpType pumpType = PumpType.CentrifugalPump;
        public CreateForm()
        {
            InitializeComponent();
            this.textBoxCode.Text = ExperimentService.GenerateExperimentCode(experimentType);
        }

        const ExperimentType experimentType = ExperimentType.NPSH;


        protected override void BandCombox()
        {
            Dictionary<ComboBox, DataSource[]> boxDic = new Dictionary<ComboBox, DataSource[]>();

            boxDic.Add(comboBoxFlowmeter, DataSource.DataSourceDic[DataSource.FLOWMETER]);

            boxDic.Add(comboBoxWindingConnection, DataSource.DataSourceDic[DataSource.WINDINGCONNECTION]);
            boxDic.Add(comboBoxWindingMaterial, DataSource.DataSourceDic[DataSource.WINGDINGMATERIAL]);
         

            boxDic.Add(comboBoxRateVoltage, DataSource.DataSourceDic[DataSource.MOTORRATEDVOLTAGE]);

            foreach (KeyValuePair<ComboBox, DataSource[]> pair in boxDic)
            {
                pair.Key.DataSource = pair.Value;
                pair.Key.DisplayMember = "BindMember";
                pair.Key.ValueMember = "BindValue";
                pair.Key.SelectedIndex = 0;
            }
        }

        protected override void initValidateInfo()
        {
            notNullList = new List<TextBox>();
            doubleFormateList = new List<TextBox>();
            doubleorNullFormateList = new List<TextBox>();

            intList = new List<TextBox>();

            notNullList.Add(this.textBoxCode);
            notNullList.Add(this.textBoxPumpName);
            notNullList.Add(this.textBoxPumpCode);
            notNullList.Add(this.textBoxExperimentOwner);
            notNullList.Add(this.textBoxManufacturer);
            notNullList.Add(this.textRatedFlow);
            notNullList.Add(this.textRatedHead);
            notNullList.Add(this.textRatedPower);
            notNullList.Add(this.textInDia);
            notNullList.Add(this.textOutDia);
            notNullList.Add(this.textBoxRatedSpeed);
            notNullList.Add(this.textBoxRatedCurrent);
            notNullList.Add(this.textBoxMotorEff);
            notNullList.Add(this.textBoxZInLet);
            notNullList.Add(this.textBoxZOutLet);
            doubleFormateList.Add(this.textBoxMotorEff);
            doubleFormateList.Add(this.textBoxZInLet);
            doubleFormateList.Add(this.textBoxZOutLet);


            notNullList.Add(this.textRatedNpsh);
            notNullList.Add(this.textCriticalNpsh);
            doubleFormateList.Add(this.textRatedFlow);
            doubleFormateList.Add(this.textRatedHead);
            doubleFormateList.Add(this.textRatedPower);
            doubleFormateList.Add(this.textInDia);
            doubleFormateList.Add(this.textOutDia);
            doubleFormateList.Add(this.textBoxRatedSpeed);
            doubleFormateList.Add(this.textBoxRatedCurrent);


            doubleFormateList.Add(this.textBoxZInLet);
            doubleFormateList.Add(this.textBoxZOutLet);
            doubleFormateList.Add(this.textRatedNpsh);
            doubleFormateList.Add(this.textCriticalNpsh);


        }

        protected override Experiment buildExperiment()
        {
            NpshExperiment experiment = new NpshExperiment();
            experiment.ExperimentCode = this.textBoxCode.Text;
            experiment.PumpName = this.textBoxPumpName.Text;
            //experiment.ExperimentName = this.textBoxPumpName.Text;
            experiment.PumpModel = this.textBoxProductModel.Text;
            experiment.PumpCode = this.textBoxPumpCode.Text;
            experiment.ExperimentOwner = this.textBoxExperimentOwner.Text;
            experiment.Manufacturer = this.textBoxManufacturer.Text;


           
            experiment.ExperimentType = ExperimentType.NPSH;
            experiment.CreateTime = DateTime.Now;
            //experiment.PumpType = pumpType;//普通离心泵
            experiment.Customer = this.textBoxCustomer.Text;

            experiment.PumpRatedFlow = Convert.ToDouble(this.textRatedFlow.Text);
            experiment.PumpRatedHead = Convert.ToDouble(this.textRatedHead.Text);
            experiment.PumpRatedNpsh = Convert.ToDouble(this.textRatedNpsh.Text);
            experiment.PumpRatedPower = Convert.ToDouble(this.textRatedPower.Text);
            //experiment.DesignCriticalNpsh = Convert.ToDouble(this.textCriticalNpsh.Text);
            experiment.MotorRatedEfficiency = Convert.ToDouble(this.textRatedEff.Text);
            experiment.DInlet = Convert.ToDouble(this.textInDia.Text)/1000;
            experiment.DOutlet = Convert.ToDouble(this.textOutDia.Text)/1000;



            experiment.MotorRatedSpeed =    Convert.ToDouble(this.textBoxRatedSpeed.Text);       
            experiment.MotorRatedVoltage =  (MotorRatedVoltage)Convert.ToInt32(this.comboBoxRateVoltage.SelectedValue);
            experiment.MotorRatedCurrent =  Convert.ToDouble(this.textBoxRatedCurrent.Text);
            experiment.MotorWindingConnection = (String)this.comboBoxWindingConnection.SelectedValue;
            experiment.MotorWindingMaterial =   (String)this.comboBoxWindingMaterial.SelectedValue;
            experiment.MotorRatedEfficiency =    Convert.ToDouble(this.textBoxMotorEff.Text);


            experiment.ZInlet = Convert.ToDouble(this.textBoxZInLet.Text) / 1000;
            experiment.ZOutlet = Convert.ToDouble(this.textBoxZOutLet.Text) / 1000;

            experiment.Flowmeter = comboBoxFlowmeter.Text;
            experiment.FlowmeterAddress = (int)comboBoxFlowmeter.SelectedValue;

        
            experiment.CreateTime = DateTime.Now;

            experiment.FlowmeterAddress = (int)this.comboBoxFlowmeter.SelectedValue;
            experiment.TransformationRatio = Convert.ToDouble(this.textBoxCurrentTransRatio.Text);
            experiment.Customer = this.textBoxCustomer.Text;

            return experiment;

        }


        private void btnSure_Click(object sender, EventArgs e)
        {

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {

        }
        protected override String RecentRecordFilePath => Config.BasePath + "\\" + (int)experimentType  +  "-recent.obj";

        protected override void assignRecentRecordToFace(Experiment experiment)
        {
            NpshExperiment npshExperiment = (NpshExperiment)experiment;

            this.textBoxPumpName.Text = npshExperiment.PumpName;

            this.textBoxProductModel.Text = npshExperiment.PumpModel;
            this.textBoxPumpCode.Text = npshExperiment.PumpCode;
            this.textBoxExperimentOwner.Text = npshExperiment.ExperimentOwner;
            this.textBoxManufacturer.Text = npshExperiment.Manufacturer;
            this.textBoxCustomer.Text = npshExperiment.Customer;

            this.textRatedFlow.Text = npshExperiment.PumpRatedFlow.ToString();
            this.textRatedHead.Text = npshExperiment.PumpRatedHead.ToString();
            this.textRatedNpsh.Text = npshExperiment.PumpRatedNpsh.ToString();
            this.textRatedPower.Text = (npshExperiment.PumpRatedPower).ToString();
            //this.textCriticalNpsh.Text = npshExperiment.DesignCriticalNpsh.ToString();
            this.textRatedEff.Text = npshExperiment.PumpRatedEfficiency.ToString();
            this.textInDia.Text =  (npshExperiment.DInlet*1000).ToString();
            this.textOutDia.Text = (npshExperiment.DOutlet*1000).ToString();

            this.textBoxRatedCurrent.Text = npshExperiment.MotorRatedCurrent.ToString();
            this.textBoxMotorEff.Text = npshExperiment.MotorRatedEfficiency.ToString();
 
            this.textBoxRatedSpeed.Text = npshExperiment.MotorRatedSpeed.ToString();
            this.comboBoxRateVoltage.SelectedValue = (int)npshExperiment.MotorRatedVoltage;
            this.comboBoxWindingConnection.SelectedValue = npshExperiment.MotorWindingConnection;
            this.comboBoxWindingMaterial.SelectedValue = npshExperiment.MotorWindingMaterial;

            this.textBoxZInLet.Text = (npshExperiment.ZInlet * 1000).ToString();
            this.textBoxZOutLet.Text = (npshExperiment.ZOutlet * 1000).ToString();        
            comboBoxFlowmeter.SelectedValue = npshExperiment.FlowmeterAddress;
  


            this.textBoxCurrentTransRatio.Text = npshExperiment.TransformationRatio.ToString();


        }

        private void CreateForm_Load(object sender, EventArgs e)
        {

        }

        private void changeRatedCurrent()
        {
            try
            {
                double designedPower = Convert.ToDouble(this.textRatedPower.Text);
                double ratedVoltage = Convert.ToDouble(this.comboBoxRateVoltage.SelectedValue);
                double transRadio = Config.TransFormationRadioConfig[ratedVoltage, designedPower];
                if (!Double.IsNaN(transRadio))
                {
                    this.textBoxCurrentTransRatio.Text = transRadio.ToString();
                }
                else
                {
                    this.textBoxCurrentTransRatio.Text = "";
                }
            }
            catch (FormatException ex)
            {
                ex.GetType();
                Console.WriteLine(ex.ToString());
            }
        }

        private void comboBoxRateVoltage_SelectedIndexChanged(object sender, EventArgs e)
        {
            changeRatedCurrent();
        }

        private void textRatedHead_Leave(object sender, EventArgs e)
        {
            changeRatedCurrent();
        }
    }
}
